package training.framework.business;

import java.util.List;

import training.framework.persistence.AbstractEntity;
import training.framework.persistence.AbstractEntityDao;

public abstract class AbstractEntityController <Identifier, Entity extends AbstractEntity<Identifier>, EntitySearchOptions extends AbstractEntitySearchOptions>{
	
	protected abstract AbstractEntityDao<Identifier, Entity, EntitySearchOptions> getDao();
	
	public void saveEntity(Entity entity) {
		Identifier identifier = entity.getIdentifier();
		
		if (identifier == null){
			validadeInsert(entity);
			getDao().insertEntity(entity);
		}else{
			validateUpdate(entity);
			getDao().updateEntity(entity);
		}
		
	}
	
	
	

	protected abstract void validateUpdate(Entity entity);

	protected abstract void validadeInsert(Entity entity);

	public int searchEntityCount(EntitySearchOptions options) {
		return getDao().selectEntityCount(options);
	}
	
	public List<Entity> searchEntity(EntitySearchOptions options) {
		return getDao().selectEntity(options);
	}

	public void deleteEntity(Entity entity) {
		validateDelete(entity);
		getDao().deleteEntity(entity);
	}
	
	public abstract void validateDelete(Entity entity);
}
